#include "Helicopter.h"
GLdouble Helicopter::m_Length=17.66;
GLdouble Helicopter::m_Height=4.7;
GLdouble Helicopter::m_Width=14.6;

Helicopter::Helicopter():baseShape()
,m_radPropeller(0)
{

}

void Helicopter::init()
{
	m_lists=glGenLists(3);
	m_listlen=2;
	glNewList(m_lists,GL_COMPILE);
	{
		
		GLfloat mat_ambient[]  = {1.0f, 1.0f, 0.0f, .2f};
		GLfloat mat_diffuse[]  = {1.0f, 1.0f, 0.0f, .3f};
		GLfloat mat_specular[] = {1.0f, 1.0f, 0.0f, .5f};
		GLfloat mat_emission[] = {.5f, .5f, 0.0f, .1f};
		GLfloat mat_shininess  = 10.0f;

		glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
		glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
		glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
		glMaterialfv(GL_FRONT, GL_EMISSION,  mat_emission);
		glMaterialf (GL_FRONT, GL_SHININESS, mat_shininess);

		GLdouble point1[]={.4,-6,1.1},
			point2[]={.4,-6,.2},
			point3[]={.6,-4.5,1.5},
			point4[]={.6,-4.5,.1},
			point5[]={.9,-3,3.1},
			point6[]={.9,-3,0},
			point7[]={1.0,-1,3.55},
			point8[]={1.6,-1,0},
			point9[]={1.0,1.9,3.55},
			point10[]={1.6,1.9,0},
			point11[]={.5,6,1.3},
			point12[]={.5,6,0},
			point13[]={.2,11.55,.9},
			point14[]={.2,11.55,.2};


		/*GLdouble point15[]={-.4,-6,1.1},
			point16[]={-.4,-6,.2},
			point17[]={-.6,-4.5,1.5},
			point18[]={-.6,-4.5,.1},
			point19[]={-1.2,-3,3.1},
			point20[]={-1.2,-3,0},
			point21[]={-1.2,-1,3.55},
			point22[]={-1.6,-1,0},
			point23[]={-1.2,1.9,3.55},
			point24[]={-1.6,1.9,0},
			point25[]={-.8,6,1.3},
			point26[]={-.8,6,0},
			point27[]={-.2,11.55,1},
			point28[]={-.2,11.55,.2};
	*/

		GLdouble point15[]={-point1[0],point1[1],point1[2]},
			point16[]={-point2[0],point2[1],point2[2]},
			point17[]={-point3[0],point3[1],point3[2]},
			point18[]={-point4[0],point4[1],point4[2]},
			point19[]={-point5[0],point5[1],point5[2]},
			point20[]={-point6[0],point6[1],point6[2]},
			point21[]={-point7[0],point7[1],point7[2]},
			point22[]={-point8[0],point8[1],point8[2]},
			point23[]={-point9[0],point9[1],point9[2]},
			point24[]={-point10[0],point10[1],point10[2]},
			point25[]={-point11[0],point11[1],point11[2]},
			point26[]={-point12[0],point12[1],point12[2]},
			point27[]={-point13[0],point13[1],point13[2]},
			point28[]={-point14[0],point14[1],point14[2]};

		GLdouble point29[]={.12,point13[1]+.5,point13[2]},
			point30[]={.12,point14[1]+.5,point14[2]},
			point31[]={.12,point13[1]+.5,4.5},
			point32[]={.12,point13[1]+1.9,4.5};

		GLdouble point33[]={-point29[0],point29[1],point29[2]},
			point34[]={-point30[0],point30[1],point30[2]},
			point35[]={-point31[0],point31[1],point31[2]},
			point36[]={-point32[0],point32[1],point32[2]};

		//body
		//left
		glBegin(GL_QUAD_STRIP);
		{
			glColor3ub(255u,255u,0);
			

			setNormal(point16,point15,point17);
			glVertex3dv(point16);
			glVertex3dv(point15);

			setNormal(point15,point18,point17);
			glVertex3dv(point18);
			glVertex3dv(point17);

			setNormal(point17,point20,point19);
			glVertex3dv(point20);
			glVertex3dv(point19);

			setNormal(point19,point22,point21);
			glVertex3dv(point22);
			glVertex3dv(point21);

			setNormal(point21,point24,point23);
			glVertex3dv(point24);
			glVertex3dv(point23);
			
			setNormal(point23,point26,point25);
			glVertex3dv(point26);
			glVertex3dv(point25);

			setNormal(point25,point28,point27);
			glVertex3dv(point28);
			glVertex3dv(point27);
		}
		glEnd();

		//right
		glBegin(GL_QUAD_STRIP);
		{
			
			setNormal(point1,point2,point3);
			glVertex3dv(point1);
			glVertex3dv(point2);

			setNormal(point2,point3,point4);
			glVertex3dv(point3);
			glVertex3dv(point4);

			setNormal(point4,point5,point6);
			glVertex3dv(point5);
			glVertex3dv(point6);

			setNormal(point6,point7,point8);
			glVertex3dv(point7);
			glVertex3dv(point8);

			setNormal(point8,point9,point10);
			glVertex3dv(point9);
			glVertex3dv(point10);

			setNormal(point10,point11,point12);
			glVertex3dv(point11);
			glVertex3dv(point12);

			setNormal(point12,point13,point14);
			glVertex3dv(point13);
			glVertex3dv(point14);
		}
		glEnd();

		//top
		glBegin(GL_QUAD_STRIP);
		{
			setNormal(point16,point2,point1);
			glVertex3dv(point16);
			glVertex3dv(point2);
			
			setNormal(point15,point1,point3);
			glVertex3dv(point15);
			glVertex3dv(point1);

			setNormal(point1,point3,point17);
			glVertex3dv(point17);
			glVertex3dv(point3);

			setNormal(point17,point5,point19);
			glVertex3dv(point19);
			glVertex3dv(point5);			

			setNormal(point19,point7,point21);
			glVertex3dv(point21);
			glVertex3dv(point7);			

			setNormal(point21,point9,point23);
			glVertex3dv(point23);
			glVertex3dv(point9);			

			setNormal(point23,point11,point25);
			glVertex3dv(point25);
			glVertex3dv(point11);			

			setNormal(point25,point13,point27);
			glVertex3dv(point27);
			glVertex3dv(point13);			

			setNormal(point27,point14,point28);
			glVertex3dv(point28);
			glVertex3dv(point14);			
		}
		glEnd();

		//tail
		glBegin(GL_QUAD_STRIP);
		{
			setNormal(point13,point14,point29);
			glVertex3dv(point13);
			glVertex3dv(point14);

			setNormal(point14,point29,point30);
			glVertex3dv(point29);
			glVertex3dv(point30);

			setNormal(point13,point29,point31);
			glVertex3dv(point13);
			glVertex3dv(point29);

			setNormal(point29,point31,point32);
			glVertex3dv(point31);
			glVertex3dv(point32);

			setNormal(point27,point28,point33);
			glVertex3dv(point28);
			glVertex3dv(point27);

			setNormal(point27,point33,point34);
			glVertex3dv(point34);
			glVertex3dv(point33);

			setNormal(point13,point29,point31);
			glVertex3dv(point33);
			glVertex3dv(point27);

			setNormal(point29,point31,point32);
			glVertex3dv(point36);
			glVertex3dv(point35);
		}
		glEnd();
		GLdouble r=.2;
		GLdouble n=64;
		GLdouble rad=0;
		//axle
		glBegin(GL_QUAD_STRIP);
		{
			glColor3ub(125u,125u,125u);
			GLfloat mat_ambient[]  = {0.2f, 0.2f, 0.2f, 1.0f};
			GLfloat mat_diffuse[]  = {0.2f, 0.2f, 0.2f, 1.0f};
			GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
			GLfloat mat_emission[] = {.5f, 0.5f, 0.5f, 1.0f};
			GLfloat mat_shininess  = 30.0f;

			glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
			glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
			glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
			glMaterialfv(GL_FRONT, GL_EMISSION,  mat_emission);
			glMaterialf (GL_FRONT, GL_SHININESS, mat_shininess);
			for(GLdouble i=0;i<n;i++)
			{
				rad=2*M_PI/n*i;
				setNormal(cos(rad)*r,sin(rad)*r,4,
					cos(rad)*r,sin(rad)*r,3.55,
					cos(rad/i*(i+1))*r,sin(rad/i*(i+1))*r,4
					);
				glVertex3d(cos(rad)*r,sin(rad)*r,4);
				glVertex3d(cos(rad)*r,sin(rad)*r,3.55);				
			}
		}
		glEnd();

	}	
	glEndList();

	//rotor wing
	glNewList(m_lists+1,GL_COMPILE);
	{		
		glBegin(GL_QUADS);
		{
			glColor3ub(193u,205u,193u);
			GLfloat mat_ambient[]  = {0.7569f, 0.8039f, 0.7569f, 1.0f};
			GLfloat mat_diffuse[]  = {0.7569f, 0.8039f, 0.7569f, 1.0f};
			GLfloat mat_specular[] = {0.7569f, 0.8039f, 0.7569f, 1.0f};
			GLfloat mat_emission[] = {0.7569f, 0.8039f, 0.7569f, 1.0f};
			GLfloat mat_shininess  = 30.0f;

			glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
			glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
			glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
			glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);
			glMaterialf (GL_FRONT,GL_SHININESS,mat_shininess);

			setNormal(-7.3,.2,4,
				-7.3,-.2,4,
				7.3,-.2,4);
			glVertex3d(-7.3,.2,4);
			glVertex3d(-7.3,-.2,4);
			glVertex3d(7.3,-.2,4);
			glVertex3d(7.3,.2,4);

			glVertex3d(-.2,-7.3,4);
			glVertex3d(.2,-7.3,4);
			glVertex3d(.2,7.3,4);
			glVertex3d(-.2,7.3,4);
		}
		glEnd();
	}
	glEndList();

	//shadow
	m_haveShadow=0;
	glNewList(m_lists+m_listlen,GL_COMPILE);
	{
		
		glBegin(GL_QUADS);
		{
			glColor3ub(105u,105u,105u);

			GLfloat mat_ambient[]  = {0.4118f, 0.4118f, 0.4118f, 0.6f};
			GLfloat mat_diffuse[]  = {0.4118f, 0.4118f, 0.4118f, 0.4f};
			GLfloat mat_specular[] = {0.4118f, 0.4118f, 0.4118f, 0.8f};
			GLfloat mat_emission[] = {0.4118f, 0.4118f, 0.4118f, 0.1f};
			GLfloat mat_shininess  = 30.0f;

			glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
			glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
			glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
			glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);
			glMaterialf (GL_FRONT,GL_SHININESS,mat_shininess);

			glVertex3d(1.6,-6,0.55);
			glVertex3d(1.6,10.6,0.55);
			glVertex3d(-1.6,10.6,0.55);
			glVertex3d(-1.6,-6,0.55);
		}
		glEnd();	
	}
	glEndList();
}


void Helicopter::draw()
{	
	if(m_haveShadow)
	{
		glPushMatrix();
		glMatrixMode(GL_MODELVIEW);
		glTranslated(m_Position[0],m_Position[1],0);
		glRotated(m_Point,0,0,1);
		glCallList(m_lists+m_listlen);
		glPopMatrix();
	}

	glPushMatrix();
	glMatrixMode(GL_MODELVIEW);
	glTranslated(m_Position[0],m_Position[1],m_Position[2]);
	glRotated(m_Point,0,0,1);
	glCallLists(m_listlen-1,GL_INT,&m_lists);

	glRotated(m_radPropeller,0,0,1);
	glCallList(m_lists+m_listlen-1);
	glPopMatrix();	
}

void Helicopter::OnTimer(int millisec)
{
	m_radPropeller+=30;
	baseShape::OnTimer(millisec);
}